<template>
    <div class="filter-container">
        <div class="main-box" ref="mainBox" v-if="renderData.length>0">
            <span class="item" v-for="(item, index) in renderData" :key="index">
                <span class="label">{{ item.label }}：</span>
                <component
                style="width: 180px;"
                :is="components[renderComponent(item)]"
                :config="item"
                :filterInfo="filterInfo"
                ></component>
            </span>
        </div>
        <div class="opera-area">
            <div class="search-box">
                <span v-show="haveShowMore" @click="toggle" class="icon-more">
                    <el-icon v-show="!isOpenStatus"><EpArrowDown /></el-icon>
                    <el-icon v-show="isOpenStatus"><EpArrowUp /></el-icon>
                </span>
                <span class="search-txt" @click="searchHandler">搜索</span>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
interface IOption {
    filterKey: string, // 绑定的字段
    type: 'select' | 'input', // 类型（重点）
    placeholder?: string,
    label: string,  // 渲染的名字
    filterable?: true, // 是否支持过滤
    options?: []   // 选项框的选项
}
interface ICommonFiltersProps {
    renderData: IOption[];
    filterInfo: any;
}
const props = withDefaults(defineProps<ICommonFiltersProps>(), {
    renderData: ()=>[]
})
const emits = defineEmits(['search'])
const mainBox = ref<(HTMLDivElement | null)>(null);
const contextHeight = ref('50px');
const haveShowMore = ref(true);
const isOpenStatus = ref(false);
// 初始化加载
onMounted(() => {
    setTimeout(()=>{
        initStatus()
    },200)
    window.onresize=()=>{
        initStatus()
    };
})
const initStatus = ()=>{
    // 获取main-box的高度
    const height = mainBox.value!.clientHeight;
    if(height>64) {
        haveShowMore.value = true;
    } else {
        haveShowMore.value = false;
    }
}
// 切换展开收起。
const toggle = () => {
    isOpenStatus.value = !isOpenStatus.value;
    const height = mainBox.value!.clientHeight;
    if(isOpenStatus.value) {
        contextHeight.value = `${height}px`;
    } else {
        contextHeight.value = '50px';
    }
}
const componentType = new Map([
    ['select', 'SearchSelect'],
    ['input', 'SearchInput']
])
// 渲染组件
const components = {
    SearchSelect: defineAsyncComponent(() => import('./components/SearchSelect.vue')),
    SearchInput: defineAsyncComponent(() => import('./components/SearchInput.vue'))
}
const renderComponent = (item:IOption) => {
    let type = componentType.get(item.type)
    return type;
}

const searchHandler = () => {
    emits('search')
}
</script>

<style scoped lang="scss">
.filter-container {
    height: v-bind(contextHeight);
    padding: 8px 0 2px 0;
    display: flex;
    overflow: hidden;
    transition: height 0.5s ease;
    .main-box {
        flex: 16;
        height: fit-content;
        box-sizing: content-box;
        .item {
            float: left;
            height: 45px;
            line-height: 45px;
            margin-right: 10px;
            @include clearfix;
            .label {
                display: inline-block;
                width: 100px;
                text-align: right;
                color: var(--el-text-color-regular)
            }
        }
    }
    .opera-area {
        flex: 2;
        min-width: 100px;
        margin-right: 20px;
        .search-box{
            display: inline-block;
            width: auto;
            background: $deepPrimaryColor;
            color: #fff;
            border-radius: 5px;
            margin-top: 8px;
            float: right;
            @include clearfix;
            .icon-more {
                vertical-align: middle;
                display: inline-block;
                /* margin-top: 2px; */
                padding: 8px 10px;
                position: relative;
                cursor: pointer;
                ::after {
                    content: "";
                    display: inline-block;
                    position: absolute;
                    width: 1px;
                    height: 80%;
                    right: -10px;
                    background: #fff;
                }
            }
            .search-txt {
                display: inline-block;
                padding: 8px 16px;
                vertical-align: middle;
                cursor: pointer;
            }
        }
    }
}
</style>
